package parser

import (
	"imooc.com/crawler/engine"
	"regexp"
	"imooc.com/crawler_distrilbuted/config"
)

var profileUrlReg  = regexp.MustCompile(`<a[^>]*href="(http://album.zhenai.com/u/[0-9]+)"[^>]*>([^<]+)</a>`)
//下一页
var cityNextUrlReg = regexp.MustCompile(`<li[^>]*class="paging-item"><a[^>]*href="(http://www.zhenai.com/zhenghun/[^"]+)">下一页</a>`)

func ParseCity(contents []byte, _ string) engine.ParseResult  {

	all := profileUrlReg.FindAllStringSubmatch(string(contents), -1)

	var parseResult engine.ParseResult
	for _,m := range all {

		//parseResult.Items = append(parseResult.Items, "User " + name)
		parseResult.Request = append(parseResult.Request, engine.Request{
			Url:m[1],
			//通过闭包的形式解决parseProfile需要两个参数的问题
			Parser: NewParseProfile(string(m[2])),
		})
	}
	//下一页的Request
	all = cityNextUrlReg.FindAllStringSubmatch(string(contents), -1)
	for _,m := range all {
		parseResult.Request = append(parseResult.Request, engine.Request{
			Url:m[1],
			Parser: engine.NewFuncParser(ParseCity, config.ParseCity),
		})
	}

	return parseResult
}